package org.example.lease.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.example.lease.model.entities.SystemPost;
import org.example.lease.model.enums.BaseStatus;
import org.example.lease.model.result.Result;
import org.example.lease.service.SystemPostService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/24 20:35
 */
@RestController
@Tag(name = "后台用户岗位管理")
@RequestMapping("/admin/system/post")
@Slf4j
public class SystemPostController {
    @Resource
    private SystemPostService systemPostService;

    /**
     * 这里发现一个错误直接调用mp的分页查找方法发现只有systemPost原本的内容被赋值了，id等继承的信息没有被赋值
     * @param current 1
     * @param size s
     * @return result
     */
    @Operation(summary = "分页获取岗位信息")
    @GetMapping("/page")
    public Result<IPage<SystemPost>> pageSysPost(Long current, Long size){
        IPage<SystemPost> iPage = new Page<>(current, size);
        IPage<SystemPost> systemPostIPage = systemPostService.page(iPage);
        //要注意这里的打印的对象是用注解@Data生成的toString方法，toString方法不会打印父类的字段，但是父类的字段会正确赋值
        log.info("岗位分页对象里面的内容为：{}", systemPostIPage.getRecords());
        return Result.ok(systemPostIPage);
    }

    @Operation(summary = "保存或更新岗位信息")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@RequestBody SystemPost systemPost) {
        systemPostService.saveOrUpdate(systemPost);
        return Result.ok();
    }

    @DeleteMapping("/deleteById")
    @Operation(summary = "根据id删除岗位")
    public Result<?> removeById(@RequestParam Long id) {
        systemPostService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "获取全部岗位列表")
    @GetMapping("/list")
    public Result<List<SystemPost>> list() {
        List<SystemPost> list = systemPostService.list();
        return Result.ok(list);
    }

    @GetMapping("getById")
    @Operation(summary = "根据id获取岗位信息")
    public Result<SystemPost> getById(@RequestParam Long id) {
        SystemPost systemPost = systemPostService.getById(id);
        return Result.ok(systemPost);
    }

    @Operation(summary = "根据岗位id修改状态")
    @PostMapping("/updateStatusByPostId")
    public Result<?> updateStatusByPostId(@RequestParam Long id, @RequestParam BaseStatus status){
        systemPostService.update(Wrappers.lambdaUpdate(SystemPost.class)
                .eq(SystemPost::getId, id).set(SystemPost::getStatus, status));
        return Result.ok();
    }
}
